﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class UserDAL
    {
        public UserDAL(HoteContent hoteContent)
        {
            HoteContent = hoteContent;
        }

        public HoteContent HoteContent { get; }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int UserAdd(UserModel info)
        {
            try
            {
                var res = HoteContent.userModels.Count(o => o.UserName == info.UserName);
                if (res > 0)
                {
                    return -1;//用户名已存在
                }
                HoteContent.userModels.Add(info);
              return  HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户管理员登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public int Login(string UserName,string Password)
        {
            try
            {
                var res = HoteContent.userModels.Where(o => o.UserName == UserName).Count();
                if (res== 0)
                {
                    return -1;//用户名不存在
                }
                else
                {
                    var erro = HoteContent.userModels.Where(o => o.UserName == UserName && o.Password == Password).Count();
                    if (erro == 0)
                    {
                        return -2;//密码错误
                    }
                    else
                    {
                        return 1;//登陆成功
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
